package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.SysNoticeUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysNoticeUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class SysNoticeUserService extends BaseService<SysNoticeUserMapper, SysNoticeUser> {

    @Transactional(readOnly = true)
    public Page<SysNoticeUser> selectUserNotice(Page<SysNoticeUser> page, String title) throws IframeException {
        Integer userId = SystemContextHolder.getSessionContext().getUserId();
        long count = this.baseMapper.selectUserNoticeCount(userId, title);
        page.setTotal(count);
        if(count == 0l){
            page.setTotal(count);
            return page;
        }
        long start = (page.getCurrent() - 1) * page.getSize();
        long size = page.getSize();
        List<SysNoticeUser> result = this.baseMapper.selectUserNotice(userId ,title, start, size);
        page.setRecords(result);
        return page;
    }

    @Transactional(readOnly = true)
    public List<SysNoticeUser> selectUnread() throws IframeException{
        LambdaQueryWrapper<SysNoticeUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysNoticeUser>();
        lambdaQueryWrapper.eq(SysNoticeUser::getSysUserId , SystemContextHolder.getSessionContext().getUserId());
        lambdaQueryWrapper.eq(SysNoticeUser::getStatus , 0);
        return super.selectList(lambdaQueryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean updateByNoticeId(Integer noticeId) throws IframeException{
        LambdaQueryWrapper<SysNoticeUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysNoticeUser>();
        lambdaQueryWrapper.eq(SysNoticeUser::getSysUserId , SystemContextHolder.getSessionContext().getUserId()).eq(SysNoticeUser::getSysNoticeId, noticeId);
        SysNoticeUser sysNoticeUser = super.selectOne(lambdaQueryWrapper);
        if(sysNoticeUser.getStatus() == 0){
            sysNoticeUser.setStatus(1);
            return super.updateById(sysNoticeUser);
        }else{
            return true;
        }
    }

    @Transactional(readOnly = true)
   public SysNoticeUser latest() throws IframeException{
        LambdaQueryWrapper<SysNoticeUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysNoticeUser>();
        lambdaQueryWrapper.eq(SysNoticeUser::getSysUserId , SystemContextHolder.getSessionContext().getUserId()).orderByAsc(SysNoticeUser::getStatus).orderByDesc(SysNoticeUser::getCreateDate).last("LIMIT 1");
        return super.selectOne(lambdaQueryWrapper);
   }

   @Transactional(readOnly = true)
   public Long view(Integer noticeId) throws IframeException{
        LambdaQueryWrapper<SysNoticeUser> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SysNoticeUser::getSysNoticeId, noticeId).eq(SysNoticeUser::getStatus , 1);
        return super.selectCount(lambdaQueryWrapper);
   }

}
